#coding:utf8

import requests


def add_note(project_id, mr_id, body):
  """添加 一条 note（评论）"""

  url = "https://git.in.zhihu.com/api/v4/projects/%s/merge_requests/%s/notes" % (project_id, mr_id)
  print 'url:'
  print url
  payload = "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"id\"\r\n\r\n%s\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"body\"\r\n\r\n%s\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"merge_request_iid\"\r\n\r\n%s\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--" %(project_id, body, mr_id)
  headers = {
    'content-type': "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
    'PRIVATE-TOKEN': "dFevxjmpprTamNSYRwgp",
  }
  response = requests.request("POST", url, data=payload, headers=headers)
  print 'response:'
  print response
  jsonObject = response.json()
  note_id = jsonObject['id']
  # print "add_note: %s" % note_id
  return str(note_id)


def update_note(project_id, mr_id, note_id, body):
  """修改 一条 note（评论）"""

  url = "https://git.in.zhihu.com/api/v4/projects/%s/merge_requests/%s/notes/%s" % (project_id, mr_id, note_id)
  payload = "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"id\"\r\n\r\n%s\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"body\"\r\n\r\n%s\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"merge_request_iid\"\r\n\r\n%s\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"note_id\"\r\n\r\n%s\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--" %(project_id, body, mr_id, note_id)

  headers = {
    'content-type': "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
    'PRIVATE-TOKEN': "dFevxjmpprTamNSYRwgp"
  }
  response = requests.request("PUT", url, data=payload, headers=headers)


def del_note(project_id, mr_id, note_id):
  """删除 一条 note（评论）"""

  url = "https://git.in.zhihu.com/api/v4/projects/%s/merge_requests/%s/notes/%s" % (project_id, mr_id, note_id)

  payload = "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"id\"\r\n\r\n%s\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"merge_request_iid\"\r\n\r\n%s\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"note_id\"\r\n\r\n%s\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--" % (project_id, mr_id, note_id)
  headers = {
    'content-type': "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
    'PRIVATE-TOKEN': "dFevxjmpprTamNSYRwgp",
    'cache-control': "no-cache",
    'Postman-Token': "f18d2905-9bc4-4baf-92e4-f25cf6779994"
  }
  response = requests.request("DELETE", url, data=payload, headers=headers)


def get_note(project_id, mr_id, note_id):
  """获取 一条 note（评论）的内容"""

  url = "https://git.in.zhihu.com/api/v4/projects/%s/merge_requests/%s/notes/%s" % (project_id, mr_id, note_id)

  headers = {
    'Content-Type': "application/json",
    'PRIVATE-TOKEN': "dFevxjmpprTamNSYRwgp",
  }

  response = requests.request("GET", url, headers=headers)
  return response.json().get('body')


if __name__ == '__main__':
  # 1、add note
  # note_id = add_note(849, 8687, '----------')
  # note_id = add_note(849, 8687, '| key | value |\n| ---------- | ------------|\n| wocao | ![qrcode](https://chart.googleapis.com/chart?chl=http://tinyurl.com/yajn8jdv&chs=100x100&cht=qr) |\n| wori | haha |')
  # print note_id

  # 2、update note
  # update_note(849, 8687, 1818577, '| key | value |\n|----|----|\n| 1 | 2 |\n| 3 | 6 |\n')

  # 3、delete note
  # del_note(849, 8687, 1797669)

  # 4、get note
  # print del_note(849, 8687, 1797170)
  pass